#!/usr/bin/python
# -*- coding:utf-8 -*-

# import Queue
import platform
import threading
import time
import sys
import os
import urllib
import re
import codecs
import shutil
import logging
import importlib
from PIL import Image
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.keys import Keys #导入模拟点击

# 实现进度说明：
# 1. selenium只能截取整个网页，不能针对单个element进行截图。
# 2. 然后进行逐页裁剪，但是页面的加载不是一次性加载，导致截图出来的只有当前页往下3页有内容，裁剪为空白。
# 3. 实现遇到问题。
# 为实现方案：
# 1. 保存每一页图片
#   https://wkretype.bdimg.com/retype/zoom/8ebd5810650e52ea54189809?pn=2&o=jpg_6&md5sum=2060e51f7e014705b8da0d9bafd125de&sign=6eb00c7ec8&png=243-485&jpg=51342-75313

importlib.reload(sys)
sys.path.append('../')

from base import utils
# sys.setdefaultencoding("utf-8")

def __get_browser():
    try:
        # dcap = dict(DesiredCapabilities.PHANTOMJS)  #设置userAgent
        # dcap["phantomjs.page.settings.userAgent"] = ("Mozilla/5.0 (X11; CrOS i686 4319.74.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36")

        options = webdriver.ChromeOptions()
        #options.binary_location("")
        # chrome config start
        # options._binary_location = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
        # chrome config end
        options.add_argument('lang=zh_CN.UTF-8')
        # options.add_argument('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/535.11')

        # profile = {"plugins.plugins_enabled": ['Chrome PDF Viewer'],
        #           "plugins.plugins_enabled": ['Adobe Flash Player'],
        #           "profile.managed_default_content_settings.images": 1,
         #          "profile.default_content_settings.popups": 0}
        #options.add_experimental_option("prefs", profile)
        options.add_argument('--start-maximized')
        options.add_argument('--enable-profile-shortcut-manager')
        options.add_argument("user-data-dir=C:/Users/Administrator/AppData/Local/Google/Chrome/User Data/Default")
        sysstr = platform.system()
        if sysstr == "Windows":
            browser = webdriver.PhantomJS(executable_path='C:\Python27\Scripts\phantomjs.exe')
            # browser = webdriver.PhantomJS()
            # chrome config
            # browser = webdriver.Chrome(chrome_options = options)
        else:
            # chrome config
            # browser = webdriver.Chrome(chrome_options=options)
            browser = webdriver.PhantomJS('phantomjs')

        browser.implicitly_wait(30)  ##设置超时时间
        browser.set_page_load_timeout(30)  ##设置超时时间
        browser.maximize_window()  # 设置全屏
        #browser.set_window_rect(100, 100, 400, 400)
        return browser
    except Exception as e:
        logging.error("failed: %s" % (e))
        return False

def cut_image(browser, file_dir, page_num):
    # 全屏浏览(//*[@id="reader-container-1"]/a)
    browser.find_element_by_xpath('//*[@id="reader-container-1"]/a').click()

    try:
        display_all = browser.find_element_by_xpath('//*[@id="html-reader-go-more"]/div[2]/span/span[2]')
        browser.execute_script("arguments[0].scrollIntoView();", display_all)
        display_all.click()
    except Exception as e:
        logging.error("failed: %s" % (e))
        return 0

    # 移动到元素element对象的“底端”与当前窗口的“底部”对齐
    #browser.execute_script("window.scrollTo(0, document.body.scrollHeight)")
    browser.execute_script("window.scrollTo(0, 0)")
    time.sleep(1)

    for num in range(1, page_num + 1):
        try:
            image_file_path = file_dir + '/page-' + str(num) + '.png'
            browser.save_screenshot(image_file_path)
            im = Image.open(image_file_path)
            curr_page_element = browser.find_element_by_id(page_ele)
            left = curr_page_element.location['x']
            top = curr_page_element.location['y']
            right = curr_page_element.location['x'] + curr_page_element.size['width']
            bottom = curr_page_element.location['y'] + curr_page_element.size['height']
            page_ele = 'pageNo-' + str(num)
            im = im.crop((left, top, right, bottom))
            im.save(image_file_path)
        except Exception as e:
            logging.error("get page %s failed" % num)
            return num
    return page_num

def conv_word(browser, file_dir, page_num):
    pass

def download(url):
    # find file id
    search_obj = re.search(r'.*?sxts=([\w-]+)', url, re.I)
    if search_obj:
        file_id = search_obj.group(1)
    else:
        logging.error("get file id failed from url:%s" % url)
        return False

    # create directory
    try:
        if os.path.exists(file_id):
            shutil.rmtree(file_id)
        os.mkdir(file_id)
    except Exception as e:
        logging.error("failed: %s" % (e))
        return False

    # cut file image
    browser = __get_browser()
    try:
        browser.get(url)
        # 获取文档类型
        doc_type = browser.find_element_by_xpath('//*[@id="doc-header-test"]/h1/b').get_attribute("class")
        if re.search(r'pdf', doc_type, re.I):
            doc_type = 'IMAGE'
        elif re.search(r'txt', doc_type, re.I):
            doc_type = 'TEXT'
        elif re.search(r'doc', doc_type, re.I):
            doc_type = 'TEXT'
        elif re.search(r'ppt', doc_type, re.I):
            doc_type = 'IMAGE'
        else:
            logging.error("don't support document type: " % doc_type)
            return

        # 获取文件页数
        doc_info = browser.find_element_by_xpath('//*[@id="hd"]/script').get_attribute('innerHTML')
        search_obj = re.search(r'.*?\'totalPageNum\': \'([\w-]+)\',', doc_info, re.I)
        if search_obj:
            page_num = int(search_obj.group(1))
        else:
            logging.error('get document page number failed')
            return

        # 关闭上次阅读提醒
        try:
            browser.find_element_by_xpath('/html/body/div[16]/div/div[2]/a[2]').click()
        except Exception as e:
            pass

        if doc_type == 'IMAGE':
            # IMAGE 图片类型，截图转换成PDF
            download_num = cut_image(browser, file_id, page_num)
            if download_num != page_num:
                logging.error('download file: %s failed, total=%s, download=%s' % (url, page_num, download_num))
        elif doc_type == 'TEXT':
            download_num = cut_image(browser, file_id, page_num)
            if download_num != page_num:
                logging.error('download file: %s failed, total=%s, download=%s' % (url, page_num, download_num))
            # TEXT 文本类型，转换成word，未实现
            # conv_word(browser, file_id, page_num)
        else:
            pass
    except Exception as e:
        logging.error("failed: %s" % (e))
        return False

    browser.quit()

if __name__ == "__main__":
    # download("https://wenku.baidu.com/view/443fa25c804d2b160b4ec01f.html?sxts=1525673013039")
    download("https://wenku.baidu.com/view/3f02d580f90f76c660371a4f.html")

